package com.zhiyun.yycall;

import com.zhiyun.trace.TraceActivity;

import android.content.Context;
import android.graphics.PixelFormat;
import android.graphics.drawable.AnimationDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;

public class YYCallScreen2 {
	private WindowManager wm;
	private TextView messageView;

	private View contentView;

	private static YYCallScreen2 instance = new YYCallScreen2();

	public static YYCallScreen2 getInstance() {
		return instance;
	}

	public void init(Context ctx) {

		createPopWindow(ctx);
		contentView.setVisibility(View.GONE);

	}

	public void show(Context ctx, String msg) {

		// if (contentView == null) {
		// this.createPopWindow(ctx);
		// }
		contentView.setVisibility(View.VISIBLE);

		messageView.setText(msg);

	}

	public void hide() {
		// if (contentView != null) {
		// wm.removeViewImmediate(contentView);
		// }
		// contentView = null;
		contentView.setVisibility(View.GONE);
	}

	public void startAnimation() {

		final ImageView popupTop = (ImageView) contentView
				.findViewById(R.id.popup_top);
		contentView.post(new Runnable() {

			@Override
			public void run() {
				AnimationDrawable anim = (AnimationDrawable) popupTop
						.getBackground();
				anim.start();

			}
		});

	}

	public void stopAnimation() {

		final ImageView popupTop = (ImageView) contentView
				.findViewById(R.id.popup_top);
		contentView.post(new Runnable() {

			@Override
			public void run() {
				AnimationDrawable anim = (AnimationDrawable) popupTop
						.getBackground();

				anim.stop();

			}
		});
	}

	private void createPopWindow(final Context ctx) {

		wm = (WindowManager) ctx.getApplicationContext().getSystemService(
				Context.WINDOW_SERVICE);
		WindowManager.LayoutParams params = new WindowManager.LayoutParams();
		// params.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
		// params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
		// | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

		// 以屏幕左上角为原点，设置x、y初始值
		params.x = 0;
		params.y = 0;

		params.type = LayoutParams.TYPE_PHONE;
		params.width = WindowManager.LayoutParams.FILL_PARENT;
		params.height = WindowManager.LayoutParams.FILL_PARENT;

		params.format = PixelFormat.RGBA_8888;

		contentView = LayoutInflater.from(ctx).inflate(
				R.layout.yycall_popup_window, null);
		wm.addView(contentView, params);
		messageView = (TextView) contentView.findViewById(R.id.content);

		final ImageView popupTop = (ImageView) contentView
				.findViewById(R.id.popup_top);
		popupTop.setBackgroundResource(R.anim.popup_top_anim);
		startAnimation();

		contentView.findViewById(R.id.end_call_btn).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {

						TraceActivity.traceEvent(ctx, "hangoff");
						// YYCallController.hangupCall(ctx);

					}
				});
	}

}
